package c.c.b.b.f;

import android.os.SystemClock;
import com.pgl.sys.ces.out.ISdkLite;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/* compiled from: DiskBasedCache */
public class j implements c.c.b.b.i.b {
    public final Map<String, a> a = new LinkedHashMap(16, 0.75f, true);
    public long b = 0;
    public final File c;
    public final int d;

    /* compiled from: DiskBasedCache */
    public static class a {
        public long a;
        public final String b;
        public final String c;
        public final long d;
        public final long e;
        public final long f;
        public final long g;
        public final List<a> h;

        public a(String str, String str2, long j, long j2, long j3, long j4, List<a> list) {
            this.b = str;
            if ("".equals(str2)) {
                str2 = null;
            }
            this.c = str2;
            this.d = j;
            this.e = j2;
            this.f = j3;
            this.g = j4;
            this.h = list;
        }

        public static a a(b bVar) throws Throwable {
            if (j.a(bVar) == 538247942) {
                String c = j.c(bVar);
                String c2 = j.c(bVar);
                long j = j.j(bVar);
                long j2 = j.j(bVar);
                long j3 = j.j(bVar);
                long j4 = j.j(bVar);
                int a = j.a(bVar);
                if (a >= 0) {
                    ArrayList emptyList = a == 0 ? Collections.emptyList() : new ArrayList();
                    for (int i = 0; i < a; i++) {
                        emptyList.add(new a(j.c(bVar).intern(), j.c(bVar).intern()));
                    }
                    return new a(c, c2, j, j2, j3, j4, emptyList);
                }
                throw new IOException(c.b.a.a.a.A("readHeaderList size=", a));
            }
            throw new IOException();
        }

        public c.c.b.b.i.b.a b(byte[] bArr) {
            c.c.b.b.i.b.a aVar = new c.c.b.b.i.b.a();
            aVar.b = bArr;
            aVar.c = this.c;
            aVar.d = this.d;
            aVar.e = this.e;
            aVar.f = this.f;
            aVar.g = this.g;
            List<a> list = this.h;
            TreeMap treeMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
            if (list != null) {
                for (a aVar2 : list) {
                    treeMap.put(aVar2.a, aVar2.b);
                }
            }
            aVar.h = treeMap;
            aVar.i = Collections.unmodifiableList(this.h);
            return aVar;
        }

        public boolean c(OutputStream outputStream) {
            try {
                j.d(outputStream, 538247942);
                j.f(outputStream, this.b);
                String str = this.c;
                if (str == null) {
                    str = "";
                }
                j.f(outputStream, str);
                j.e(outputStream, this.d);
                j.e(outputStream, this.e);
                j.e(outputStream, this.f);
                j.e(outputStream, this.g);
                List<a> list = this.h;
                if (list != null) {
                    j.d(outputStream, list.size());
                    for (a aVar : list) {
                        j.f(outputStream, aVar.a);
                        j.f(outputStream, aVar.b);
                    }
                } else {
                    j.d(outputStream, 0);
                }
                outputStream.flush();
                return true;
            } catch (Throwable th) {
                q.c("%s", th.toString());
                return false;
            }
        }

        public a(String str, c.c.b.b.i.b.a aVar) {
            List list;
            String str2 = aVar.c;
            long j = aVar.d;
            long j2 = aVar.e;
            long j3 = aVar.f;
            long j4 = aVar.g;
            List list2 = aVar.i;
            if (list2 != null) {
                list = list2;
            } else {
                ArrayList arrayList;
                Map map = aVar.h;
                if (map == null) {
                    arrayList = new ArrayList();
                } else {
                    ArrayList arrayList2 = new ArrayList(map.size());
                    for (Entry entry : map.entrySet()) {
                        arrayList2.add(new a((String) entry.getKey(), (String) entry.getValue()));
                    }
                    arrayList = arrayList2;
                }
                list = arrayList;
            }
            this(str, str2, j, j2, j3, j4, list);
        }
    }

    /* compiled from: DiskBasedCache */
    public static class b extends FilterInputStream {
        public final long a;
        public long b;

        public b(InputStream inputStream, long j) {
            super(inputStream);
            this.a = j;
        }

        public int read() throws IOException {
            int read = super.read();
            if (read != -1) {
                this.b++;
            }
            return read;
        }

        public int read(byte[] bArr, int i, int i2) throws IOException {
            int read = super.read(bArr, i, i2);
            if (read != -1) {
                this.b += (long) read;
            }
            return read;
        }
    }

    public j(File file) {
        this.c = file;
        this.d = 5242880;
    }

    public static int a(InputStream inputStream) throws Throwable {
        return (m(inputStream) << 24) | ((((m(inputStream) << 0) | 0) | (m(inputStream) << 8)) | (m(inputStream) << 16));
    }

    public static String c(b bVar) throws Throwable {
        return new String(i(bVar, j(bVar)), "UTF-8");
    }

    public static void d(OutputStream outputStream, int i) throws Throwable {
        outputStream.write((i >> 0) & ISdkLite.REGION_UNSET);
        outputStream.write((i >> 8) & ISdkLite.REGION_UNSET);
        outputStream.write((i >> 16) & ISdkLite.REGION_UNSET);
        outputStream.write((i >> 24) & ISdkLite.REGION_UNSET);
    }

    public static void e(OutputStream outputStream, long j) throws Throwable {
        outputStream.write((byte) ((int) (j >>> null)));
        outputStream.write((byte) ((int) (j >>> 8)));
        outputStream.write((byte) ((int) (j >>> 16)));
        outputStream.write((byte) ((int) (j >>> 24)));
        outputStream.write((byte) ((int) (j >>> 32)));
        outputStream.write((byte) ((int) (j >>> 40)));
        outputStream.write((byte) ((int) (j >>> 48)));
        outputStream.write((byte) ((int) (j >>> 56)));
    }

    public static void f(OutputStream outputStream, String str) throws Throwable {
        byte[] bytes = str.getBytes("UTF-8");
        e(outputStream, (long) bytes.length);
        outputStream.write(bytes, 0, bytes.length);
    }

    public static byte[] i(b bVar, long j) throws Throwable {
        long j2 = bVar.a - bVar.b;
        if (j >= 0 && j <= j2) {
            int i = (int) j;
            if (((long) i) == j) {
                byte[] bArr = new byte[i];
                new DataInputStream(bVar).readFully(bArr);
                return bArr;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("streamToBytes length=");
        stringBuilder.append(j);
        stringBuilder.append(", maxLength=");
        stringBuilder.append(j2);
        throw new IOException(stringBuilder.toString());
    }

    public static long j(InputStream inputStream) throws Throwable {
        return (((((((((((long) m(inputStream)) & 255) << null) | 0) | ((((long) m(inputStream)) & 255) << 8)) | ((((long) m(inputStream)) & 255) << 16)) | ((((long) m(inputStream)) & 255) << 24)) | ((((long) m(inputStream)) & 255) << 32)) | ((((long) m(inputStream)) & 255) << 40)) | ((((long) m(inputStream)) & 255) << 48)) | ((255 & ((long) m(inputStream))) << 56);
    }

    public static int m(InputStream inputStream) throws Throwable {
        int read = inputStream.read();
        if (read != -1) {
            return read;
        }
        throw new EOFException();
    }

    /* JADX WARNING: Removed duplicated region for block: B:49:0x009f A:{SYNTHETIC, Splitter:B:49:0x009f} */
    /* JADX WARNING: Missing exception handler attribute for start block: B:58:0x00aa */
    public synchronized c.c.b.b.i.b.a b(java.lang.String r12) {
        /*
        r11 = this;
        monitor-enter(r11);
        r0 = android.text.TextUtils.isEmpty(r12);	 Catch:{ all -> 0x00ab }
        r1 = 0;
        if (r0 == 0) goto L_0x000a;
    L_0x0008:
        monitor-exit(r11);
        return r1;
    L_0x000a:
        r0 = r11.a;	 Catch:{ all -> 0x00ab }
        r0 = r0.get(r12);	 Catch:{ all -> 0x00ab }
        r0 = (c.c.b.b.f.j.a) r0;	 Catch:{ all -> 0x00ab }
        if (r0 != 0) goto L_0x0016;
    L_0x0014:
        monitor-exit(r11);
        return r1;
    L_0x0016:
        r2 = r11.n(r12);	 Catch:{ all -> 0x00ab }
        r3 = 1;
        r4 = 0;
        r5 = 2;
        r6 = new c.c.b.b.f.j$b;	 Catch:{ all -> 0x0085 }
        r7 = new java.io.BufferedInputStream;	 Catch:{ all -> 0x0085 }
        r8 = new java.io.FileInputStream;	 Catch:{ all -> 0x0085 }
        r8.<init>(r2);	 Catch:{ all -> 0x0085 }
        r7.<init>(r8);	 Catch:{ all -> 0x0085 }
        r8 = r2.length();	 Catch:{ all -> 0x0085 }
        r6.m1017init(r7, r8);	 Catch:{ all -> 0x0085 }
        r7 = c.c.b.b.f.j.a.a(r6);	 Catch:{ all -> 0x007e }
        r8 = r7.b;	 Catch:{ all -> 0x007e }
        r8 = android.text.TextUtils.equals(r12, r8);	 Catch:{ all -> 0x007e }
        if (r8 != 0) goto L_0x0069;
    L_0x003c:
        r0 = "%s: key=%s, found=%s";
        r8 = 3;
        r8 = new java.lang.Object[r8];	 Catch:{ all -> 0x007e }
        r9 = r2.getAbsolutePath();	 Catch:{ all -> 0x007e }
        r8[r4] = r9;	 Catch:{ all -> 0x007e }
        r8[r3] = r12;	 Catch:{ all -> 0x007e }
        r7 = r7.b;	 Catch:{ all -> 0x007e }
        r8[r5] = r7;	 Catch:{ all -> 0x007e }
        c.c.b.b.f.q.c(r0, r8);	 Catch:{ all -> 0x007e }
        r0 = r11.a;	 Catch:{ all -> 0x007e }
        r0 = r0.remove(r12);	 Catch:{ all -> 0x007e }
        r0 = (c.c.b.b.f.j.a) r0;	 Catch:{ all -> 0x007e }
        if (r0 == 0) goto L_0x0061;
    L_0x005a:
        r7 = r11.b;	 Catch:{ all -> 0x007e }
        r9 = r0.a;	 Catch:{ all -> 0x007e }
        r7 = r7 - r9;
        r11.b = r7;	 Catch:{ all -> 0x007e }
    L_0x0061:
        r6.close();	 Catch:{ all -> 0x0083 }
        r6.close();	 Catch:{ all -> 0x0067 }
    L_0x0067:
        monitor-exit(r11);
        return r1;
    L_0x0069:
        r7 = r6.a;	 Catch:{ all -> 0x007e }
        r9 = r6.b;	 Catch:{ all -> 0x007e }
        r7 = r7 - r9;
        r7 = i(r6, r7);	 Catch:{ all -> 0x007e }
        r0 = r0.b(r7);	 Catch:{ all -> 0x007e }
        r6.close();	 Catch:{ all -> 0x0083 }
        r6.close();	 Catch:{ all -> 0x007c }
    L_0x007c:
        monitor-exit(r11);
        return r0;
    L_0x007e:
        r0 = move-exception;
        r6.close();	 Catch:{ all -> 0x0083 }
        throw r0;	 Catch:{ all -> 0x0083 }
    L_0x0083:
        r0 = move-exception;
        goto L_0x0087;
    L_0x0085:
        r0 = move-exception;
        r6 = r1;
    L_0x0087:
        r7 = "%s: %s";
        r5 = new java.lang.Object[r5];	 Catch:{ all -> 0x00a4 }
        r2 = r2.getAbsolutePath();	 Catch:{ all -> 0x00a4 }
        r5[r4] = r2;	 Catch:{ all -> 0x00a4 }
        r0 = r0.toString();	 Catch:{ all -> 0x00a4 }
        r5[r3] = r0;	 Catch:{ all -> 0x00a4 }
        c.c.b.b.f.q.c(r7, r5);	 Catch:{ all -> 0x00a4 }
        r11.l(r12);	 Catch:{ all -> 0x00a4 }
        if (r6 == 0) goto L_0x00a2;
    L_0x009f:
        r6.close();	 Catch:{ all -> 0x00a2 }
    L_0x00a2:
        monitor-exit(r11);
        return r1;
    L_0x00a4:
        r12 = move-exception;
        if (r6 == 0) goto L_0x00aa;
    L_0x00a7:
        r6.close();	 Catch:{ all -> 0x00aa }
    L_0x00aa:
        throw r12;	 Catch:{ all -> 0x00ab }
    L_0x00ab:
        r12 = move-exception;
        monitor-exit(r11);
        throw r12;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.b.b.f.j.b(java.lang.String):c.c.b.b.i.b$a");
    }

    public final void g(String str, a aVar) {
        if (this.a.containsKey(str)) {
            a aVar2 = (a) this.a.get(str);
            this.b = (aVar.a - aVar2.a) + this.b;
        } else {
            this.b += aVar.a;
        }
        this.a.put(str, aVar);
    }

    /* JADX WARNING: Removed duplicated region for block: B:30:0x0068 A:{SYNTHETIC, Splitter:B:30:0x0068} */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x0071  */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x0071  */
    /* JADX WARNING: Missing exception handler attribute for start block: B:32:0x006b */
    /* JADX WARNING: Can't wrap try/catch for region: R(8:27|26|(2:30|31)|32|33|(1:35)|36|37) */
    public synchronized void h(java.lang.String r8, c.c.b.b.i.b.a r9) {
        /*
        r7 = this;
        monitor-enter(r7);
        r0 = r7.b;	 Catch:{ all -> 0x0080 }
        r2 = r9.b;	 Catch:{ all -> 0x0080 }
        r3 = r2.length;	 Catch:{ all -> 0x0080 }
        r3 = (long) r3;	 Catch:{ all -> 0x0080 }
        r0 = r0 + r3;
        r3 = r7.d;	 Catch:{ all -> 0x0080 }
        r4 = (long) r3;	 Catch:{ all -> 0x0080 }
        r6 = (r0 > r4 ? 1 : (r0 == r4 ? 0 : -1));
        if (r6 <= 0) goto L_0x001d;
    L_0x000f:
        r0 = r2.length;	 Catch:{ all -> 0x0080 }
        r0 = (float) r0;
        r1 = (float) r3;
        r2 = 1063675494; // 0x3f666666 float:0.9 double:5.2552552E-315;
        r1 = r1 * r2;
        r0 = (r0 > r1 ? 1 : (r0 == r1 ? 0 : -1));
        if (r0 <= 0) goto L_0x001d;
    L_0x001b:
        monitor-exit(r7);
        return;
    L_0x001d:
        r0 = r7.n(r8);	 Catch:{ all -> 0x0080 }
        r1 = 0;
        r2 = 0;
        r3 = 1;
        r4 = new java.io.BufferedOutputStream;	 Catch:{ all -> 0x0066 }
        r5 = new java.io.FileOutputStream;	 Catch:{ all -> 0x0066 }
        r5.<init>(r0);	 Catch:{ all -> 0x0066 }
        r4.<init>(r5);	 Catch:{ all -> 0x0066 }
        r1 = new c.c.b.b.f.j$a;	 Catch:{ all -> 0x0065 }
        r1.m1015init(r8, r9);	 Catch:{ all -> 0x0065 }
        r5 = r1.c(r4);	 Catch:{ all -> 0x0065 }
        if (r5 == 0) goto L_0x004f;
    L_0x0039:
        r9 = r9.b;	 Catch:{ all -> 0x0065 }
        r4.write(r9);	 Catch:{ all -> 0x0065 }
        r5 = r0.length();	 Catch:{ all -> 0x0065 }
        r1.a = r5;	 Catch:{ all -> 0x0065 }
        r7.g(r8, r1);	 Catch:{ all -> 0x0065 }
        r7.k();	 Catch:{ all -> 0x0065 }
        r4.close();	 Catch:{ all -> 0x004d }
    L_0x004d:
        monitor-exit(r7);
        return;
    L_0x004f:
        r4.close();	 Catch:{ all -> 0x0065 }
        r8 = "Failed to write header for %s";
        r9 = new java.lang.Object[r3];	 Catch:{ all -> 0x0065 }
        r1 = r0.getAbsolutePath();	 Catch:{ all -> 0x0065 }
        r9[r2] = r1;	 Catch:{ all -> 0x0065 }
        c.c.b.b.f.q.c(r8, r9);	 Catch:{ all -> 0x0065 }
        r8 = new java.io.IOException;	 Catch:{ all -> 0x0065 }
        r8.<init>();	 Catch:{ all -> 0x0065 }
        throw r8;	 Catch:{ all -> 0x0065 }
    L_0x0065:
        r1 = r4;
    L_0x0066:
        if (r1 == 0) goto L_0x006b;
    L_0x0068:
        r1.close();	 Catch:{ all -> 0x006b }
    L_0x006b:
        r8 = r0.delete();	 Catch:{ all -> 0x0080 }
        if (r8 != 0) goto L_0x007e;
    L_0x0071:
        r8 = "Could not clean up file %s";
        r9 = new java.lang.Object[r3];	 Catch:{ all -> 0x0080 }
        r0 = r0.getAbsolutePath();	 Catch:{ all -> 0x0080 }
        r9[r2] = r0;	 Catch:{ all -> 0x0080 }
        c.c.b.b.f.q.c(r8, r9);	 Catch:{ all -> 0x0080 }
    L_0x007e:
        monitor-exit(r7);
        return;
    L_0x0080:
        r8 = move-exception;
        monitor-exit(r7);
        throw r8;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.b.b.f.j.h(java.lang.String, c.c.b.b.i.b$a):void");
    }

    public final void k() {
        if (this.b >= ((long) this.d)) {
            if (q.a) {
                q.a("Pruning old cache entries.", new Object[0]);
            }
            long j = this.b;
            long elapsedRealtime = SystemClock.elapsedRealtime();
            Iterator it = this.a.entrySet().iterator();
            int i = 0;
            while (it.hasNext()) {
                a aVar = (a) ((Entry) it.next()).getValue();
                if (n(aVar.b).delete()) {
                    this.b -= aVar.a;
                } else {
                    r10 = new Object[2];
                    String str = aVar.b;
                    r10[0] = str;
                    r10[1] = o(str);
                    q.c("Could not delete cache entry for key=%s, filename=%s", r10);
                }
                it.remove();
                i++;
                if (((float) this.b) < ((float) this.d) * 0.9f) {
                    break;
                }
            }
            if (q.a) {
                q.a("pruned %d files, %d bytes, %d ms", Integer.valueOf(i), Long.valueOf(this.b - j), Long.valueOf(SystemClock.elapsedRealtime() - elapsedRealtime));
            }
        }
    }

    public synchronized void l(String str) {
        boolean delete = n(str).delete();
        a aVar = (a) this.a.remove(str);
        if (aVar != null) {
            this.b -= aVar.a;
        }
        if (!delete) {
            q.c("Could not delete cache entry for key=%s, filename=%s", str, o(str));
        }
    }

    public File n(String str) {
        return new File(this.c, o(str));
    }

    public final String o(String str) {
        int length = str.length() / 2;
        StringBuilder g = c.b.a.a.a.g(String.valueOf(str.substring(0, length).hashCode()));
        g.append(String.valueOf(str.substring(length).hashCode()));
        return g.toString();
    }
}
